using System;
using System.Collections.Generic;

namespace mynamespace.bankaccount {

    public class BankAccount {

        private static int accountNumberSeed = 1234567890;

        public string Number {get;}

        public string Owner {get;set;}

        // public decimal Balance {get;}
        public decimal Balance {
            get{
                decimal balance = 0;
                foreach(var item in allTransactions) {
                    balance += item.Amount;
                }
                return balance;
            }
        }

        /// <summary>  账户所有的交易记录
        public List<Transaction> allTransactions = new List<Transaction>();

        public BankAccount(string name, decimal initialBalance) {
            this.Owner = name;
            // this.Balance = initialBalance;
            this.Number = accountNumberSeed.ToString();
            accountNumberSeed++;

            MakeDeposit(initialBalance, DateTime.Now, "Initial balance");
            Console.WriteLine($"Create BankAccount() and Number is {Number}");
        }

        /// <summary>  存款
        public void MakeDeposit(decimal amount, DateTime date, string note) {
            Console.WriteLine("MakeDeposit");
            if(amount <= 0) {
                throw new ArgumentOutOfRangeException(nameof(amount), "Amount of deposit must be positive");
            }
            var deposit = new Transaction(amount, date, note);
            allTransactions.Add(deposit);
        }

        public void MakeWithdrawal(decimal amount, DateTime date, string note) {
            Console.WriteLine("MakeWithdrawal");
            if (amount <= 0) {
                throw new ArgumentOutOfRangeException(nameof(amount), "Amount of deposit must be positive.");
            }
            if(Balance - amount < 0) {
                throw new InvalidOperationException("Not sufficient funds for this withdrawal");
            }
            var withdrawal = new Transaction(-amount, date, note);
            allTransactions.Add(withdrawal);
        }

        public static void testBankAccount(){
            var account = new BankAccount("zhangsan", 1000);
            Console.WriteLine($"Account {account.Number} was created for {account.Owner} with {account.Balance} initial balance.");

            try{
            account.MakeWithdrawal(500, DateTime.Now, "Rent payment");
            }catch(InvalidOperationException e) {
                Console.WriteLine("Exception caught trying to overdraw");
                Console.WriteLine(e.ToString());
            }
            Console.WriteLine(account.Balance);
            try{
            account.MakeDeposit(100, DateTime.Now, "Friend paid me back");
            }catch(ArgumentOutOfRangeException e) {
                Console.WriteLine("Exception caught trying to deposit");
                Console.WriteLine(e.ToString());
            }
            Console.WriteLine(account.Balance);
        }
    }

    /// <summary> 账户的银行交易记录
    public class Transaction {
        public decimal Amount {get;}

        public DateTime Date{get;}

        public string Notes{get;}

        public Transaction(decimal amount, DateTime date, string note) {
            this.Amount = amount;
            this.Date = date;
            this.Notes = note;
        }
    }
}